/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 *Truth: I Believe in Spring Brother~
 */

package electsys;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author t
 * Created 2010-7-1  20:36:18
 * Last Modified 2010-7-1  20:36:18
 *
 */
public class LessonTable {
    public List<List<LessonInfo>> curriculum = null;
    public int MaxWeek = 0;

    public LessonTable() {
        this.curriculum = new ArrayList();
        for ( int i =0; i < 7; i++ ) {
            List<LessonInfo> tmp = new ArrayList();
            this.curriculum.add(tmp);
        }
    }

    public boolean addCourse(LessonInfo ci) {
        int iow = ci.dayOfWeek;
        if (iow<0 || iow > 6) return false;
        int index = 0;
        for (LessonInfo tmp : curriculum.get(iow)) {
            if (LessonInfo.earlier(tmp.startTime, ci.startTime)) {
                index++;
            }
            else {
                break;
            }
        }
        this.curriculum.get(iow).add(index, ci);
        if (ci.weeks.get(ci.weeks.size()-1) > this.MaxWeek) this.MaxWeek = ci.weeks.get(ci.weeks.size()-1);
        return true;
    }

    public boolean delCourse(String name) {
        return true;
    }

    public LessonInfo getCourse(String name) {
        return null;
    }

    public String getMainInfo() {
        String tmp = "";
        for (List<LessonInfo> altmp : this.curriculum)
            for (LessonInfo citmp : altmp) {
                tmp += citmp.getMainInfo() + "\n";
            }
        return tmp;
    }

    public List<String> getLessonInfos() {
        String ret;
        List<String> r = new ArrayList();
        for (List<LessonInfo> tmp : this.curriculum)
            for (LessonInfo ltmp : tmp) {
                ret = new String(ltmp.getMainInfo());
                r.add(ret);
            }
       return r;
    }

    public List<LessonInfo> getAllLessons(List<String> bsids) {
        List<LessonInfo> alllesson = new ArrayList();
        if (bsids == null) {
            for (int i = 0; i < 7; i++) {
                for (int j = 0; j < this.curriculum.get(i).size(); j++) {
                    alllesson.add(this.curriculum.get(i).get(j));
                }
            }
        } else {
            for (int i = 0; i < 7; i++) {
                for (int j = 0; j < this.curriculum.get(i).size(); j++) {
                    for (String tmp : bsids) {
                        if (tmp.equals(this.curriculum.get(i).get(j).id)) {
                            alllesson.add(this.curriculum.get(i).get(j));
                            break;
                        }
                    }
                }
            }
        }
        return alllesson;
    }
}
